/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Controller;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.sql.*;
import java.util.ArrayList;
import Module.*;
/**
 *
 * @author CCD gruppe 2
 */
public class ActivityController {
    /**
     * Database object used to connect to the database
     */
    Database database = new Database();
    /**
     * Method used to register a activity
     * @param activity
     * @return
     */
    
    public boolean registerActivity(Activity activity) {
        Connection conn = null;
        try{
            conn = DriverManager.getConnection(database.getURL());
            String[] autogen = {"id"};
            boolean b = activity.isOnActionList();
            int i=0;
            if(b)i=1;
            PreparedStatement statement = conn.prepareStatement("Insert into activity values(DEFAULT,?,?,?,?,"
                    + "?,?,?,?,?,?)", autogen);
            statement.setInt(1,activity.getProjectId());
            statement.setString(2, activity.getActivityName());
            statement.setString(3, activity.getStarteDate());
            statement.setString(4, activity.getEndDate());
            statement.setString(5, activity.getResponsible());
            statement.setString(6, activity.getStatus());
            statement.setString(7, activity.getDescription());
            statement.setString(8, activity.getGoals());
            statement.setString(9, activity.getLocation());
            statement.setInt(10,i);
            statement.executeUpdate();
            conn.close();
            return true;
        } catch(Exception e) {
            System.err.println(e);
        }

        return false;
    }
    /**
     * Method used to edit a activity
     * @param activity
     * @return
     */
    public boolean editActivity(Activity activity){
        Connection conn = null;
        try{
            conn = DriverManager.getConnection(database.getURL());
            PreparedStatement statement = conn.prepareStatement("update activity "
                    + "set projectId=?, activityName=?, startDate=?, endDate=?, responsible=?, "
                    + "status=?, description=?, goals=?, location=?, onActionList=? where activityId=?");
            boolean b = activity.isOnActionList();
            int i=0;
            if(b)i=1;
            statement.setInt(1,activity.getProjectId());
            statement.setString(2, activity.getActivityName());
            statement.setString(3, activity.getStarteDate());
            statement.setString(4, activity.getEndDate());
            statement.setString(5, activity.getResponsible());
            statement.setString(6, activity.getStatus());
            statement.setString(7, activity.getDescription());
            statement.setString(8, activity.getGoals());
            statement.setString(9, activity.getLocation());
            statement.setInt(10,i);
            statement.setInt(11,activity.getId());
            statement.executeUpdate();
            conn.close();
            return true;

        }catch(Exception e) {
            System.err.println(e);
        }

        return false;
    }
    /**
     * Method used to delete a activity
     * @param activity
     * @return
     */
    public boolean deleteActivity(Activity activity){
        Connection conn = null;
        Controller c = new Controller();
        try{
            conn = DriverManager.getConnection(database.getURL());
            PreparedStatement statement = conn.prepareStatement("delete from activity where activityId=?");
            statement.setInt(1, activity.getId());
            statement.executeUpdate();
            conn.close();
            return true;

        }catch(Exception e){
            System.err.println(e);
        }
        return false;
    }
    /**
     * Return all activitis to a session, uses the date and projectId to decied if a activity is to a session
     * @param session
     * @return
     */
    public ArrayList<Activity> getActivitysToSession(Session s){
        Connection conn = null;
        ArrayList<Activity> activities = new ArrayList<Activity>();
        try{
            conn = DriverManager.getConnection(database.getURL());
            PreparedStatement statement = conn.prepareStatement(""
                    + "select activity.* from activity "
                    + "left join session on(activity.startDate = ? and activity.endDate=?) "
                    + "where activity.projectId = ?");
            statement.setString(1,s.getDate());
            statement.setString(2,s.getDate());
            statement.setInt(3,s.getId());
            ResultSet rs = statement.executeQuery();
            boolean b=false;
            while(rs.next()){
                if(rs.getInt(11)!=0)b=true;
                else b=false;
                activities.add(new Activity(rs.getInt(1),rs.getInt(2),rs.getString(3),rs.getString(4),rs.getString(5),
                        rs.getString(6),rs.getString(7),rs.getString(8),rs.getString(9),rs.getString(10),b));
            }
            conn.close();
            return activities;
        }catch(Exception e){
            System.err.println(e);
        }


        return activities;
    }
    /**
     * Method used to return all activities to a project
     * @param project
     * @return
     */
    public ArrayList<Activity> getActivitysToProject(Project project){
        Connection conn = null;
        ArrayList<Activity> activities = new ArrayList<Activity>();
        try{
            conn = DriverManager.getConnection(database.getURL());
            PreparedStatement statement = conn.prepareStatement("select activity.* from activity "+
            "where activity.PROJECTID=? order by activity.startDate");
            statement.setInt(1, project.getId());
            ResultSet rs = statement.executeQuery();
            while(rs.next()){

                boolean b=false;
                if(rs.getBoolean(11))b=true;
                activities.add(new Activity(rs.getInt(1),rs.getInt(2),rs.getString(3), rs.getString(4), rs.getString(5),
                        rs.getString(6),rs.getString(7),rs.getString(8),rs.getString(9),rs.getString(10),b));
            }
            conn.close();
            return activities;
        }catch(Exception e){
            System.err.println(e);
        }
        return activities;
    }
}

